package thread;

public class SyncDemo4 {
    public static void main(String[] args) {
        Foo foo=new Foo();
        Thread t1=new Thread(foo::methodA);
        Thread t2=new Thread(foo::methodB);
        t1.start();
        t2.start();
    }
}
class Foo{
    public synchronized void methodA()
    {
        try {
            Thread t=Thread.currentThread();
            System.out.println(t.getName()+"正在执行methodA");
            Thread.sleep(3000);
            System.out.println(t.getName()+"执行methodA完毕");
        } catch (InterruptedException e) {
        }
    }
    public synchronized void methodB()
    {
        try {
            Thread t=Thread.currentThread();
            System.out.println(t.getName()+"正在执行methodB");
            Thread.sleep(3000);
            System.out.println(t.getName()+"执行methodB完毕");
        } catch (InterruptedException e) {
        }
    }
}
